﻿/*
 * Copyright (c) 2012, University of Southampton All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of University of Southampton nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text.RegularExpressions;

namespace LabTroveWebParts.LabTroveODataViewer
{
    [ToolboxItemAttribute(false)]
    public class LabTroveODataViewer : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/LabTroveWebParts/LabTroveODataViewer/LabTroveODataViewerUserControl.ascx";

        protected override void CreateChildControls()
        {
            LabTroveODataViewerUserControl control = Page.LoadControl(_ascxPath) as LabTroveODataViewerUserControl;
            control.ParentWebPart = this;
            Controls.Add(control);
        }

        private string _blogShortName;
        /// <summary>
        ///  The short name (sname) of the blog, this is up to 20 characters long.
        /// </summary>
        [Category("LabTrove Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDisplayName("e-Notebook Short Name"),
        WebDescription("The short name consists of up to 20 characters, including only letters, numbers and the underscore.")]
        public string blogShortName {
            get { return _blogShortName; }
            set
            {
                if (value=="" || value.Length>20) // On the 'New Blog' form, LT states that it can be 20 chars max (NB: the SQL field will admit 64 chars)
                    throw new Microsoft.SharePoint.WebPartPages.WebPartPageUserException("Please enter a valid blog short name of least 1-20 characters.");
                _blogShortName = value;
            }
        }

        private bool _publicBlog;
        /// <summary>
        /// True if the blog to be viewed is public, else false.
        /// </summary>
        [Category("LabTrove Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDisplayName("Public"),
        WebDescription("Check box if the blog is public")]
        public bool publicBlog
        {
            get { return _publicBlog; }
            set { _publicBlog = value; }
        }

        private string _blogUID;
        /// <summary>
        /// The {uid} assigned to the user
        /// </summary>
        [Category("LabTrove Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDisplayName("LabTrove UID"),
        WebDescription("Your personal LabTrove Master UID.")]
        public string blogUID {
            get { return _blogUID; }
            set
            {
                Regex uidRegex = new Regex("[a-zA-Z0-9]+");
                if (!(uidRegex.IsMatch(value) || _publicBlog))
                    throw new Microsoft.SharePoint.WebPartPages.WebPartPageUserException("Please enter an alphanumeric LabTrove Master UID.");
                else
                    _blogUID = value;
            }
        }

        private string _blogSite;
        /// <summary>
        /// The base address of the blog, e.g. http://labtrove.example.org
        /// </summary>
        [Category("LabTrove Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDisplayName("LabTrove base address"),
        WebDescription("The base address of your LabTrove installation, e.g. http://labtrove.example.org")]
        public string blogSite {
            get { return _blogSite; }
            set
            {
                Regex siteRegex = new Regex("[hH][tT][tT][pP].+");
                if (!siteRegex.IsMatch(value))
                    throw new Microsoft.SharePoint.WebPartPages.WebPartPageUserException("Please enter a URL beginning with HTTP.");
                else
                    _blogSite = value;
            }
        }

        private int _top;
        /// <summary>
        /// Display "top" posts per page.
        /// </summary>
        [Category("LabTrove Settings"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDisplayName("Posts per page"),
        WebDescription("This setting control how many posts are displayed on each page.")]
        public int top
        {
            get 
            {
                return (_top>0 ? _top : 10);
            }
            set
            {
                if (value < 1 || value > 100)
                {
                    throw new Microsoft.SharePoint.WebPartPages.WebPartPageUserException("Please enter an integer between 1 and 100.");
                }
                else
                {
                    _top = value;
                }
            }
        }
    }
}
